package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.framework.utils.ExcelUtils;
import com.tinyideatech.system.entity.MallPay;
import com.tinyideatech.system.service.IMallPayService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author zhangy-5
 * @date 2019-08-26
 */

@Controller
@RequestMapping("/mall/pay")
public class MallPayController extends BaseController {

    @Autowired
    private IMallPayService payService;

    @GetMapping()
    public String list() {
        return "mall/pay/list";
    }

    @ResponseBody
    @PostMapping("/list")
    public ApiResult list(
            MallPay pay,
            @RequestParam(required = false, defaultValue = "") Date startTime,
            @RequestParam(required = false, defaultValue = "") Date endTime
    ) {
        QueryWrapper<MallPay> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty("")) {
            qw.like("", "");
        }
        if (startTime != null) {
            qw.ge("create_time", startTime);
        }
        if (endTime != null) {
            qw.le("create_time", endTime);
        }
        List<MallPay> list = payService.list(qw);
        return success(list);
    }

    /**
     * Excel下载
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导出订单Excel")
    @GetMapping("/export/order")
    public void orderExcel(HttpServletResponse response) {
        XSSFWorkbook wb = payService.downloadRecorder();
        String fileName = "MallOrder报表.xlsx";
        ExcelUtils.init(fileName, response, wb);
    }
}
